package core.objects;

import java.util.ArrayList;
import java.util.Calendar;

import core.objects.enums.GenderEnum;
import java.sql.Timestamp;
import java.util.Date;

public class User extends BaseObject {

    private String firstName = null;
    private String lastName = null;
    private String password = null;
    private GenderEnum gender = null;
    private Calendar birthday = null;
    private int cityID = 0;
    private int countryID = 0;
    private boolean isAdmin = false;
    private ArrayList<Book> books = null;
    private Timestamp registerTime = null;
    private byte[] image = null;
    private String email = null;

    public User(int id, String username){
        super(id, username);
    }
    
    public User(int id, String username, String firstName, String lastName, String pass, GenderEnum gender, 
            Calendar birthday, int cityId, int countryId, byte[] image, String email){
        super(id, username);
        setUserProperties(firstName,lastName,pass,gender,birthday,cityId,countryId,image, email);
        this.registerTime = new Timestamp(new Date().getTime());
    }
    
    public User(int id, String username, String firstName, String lastName,
            String pass, GenderEnum gender, Calendar birthday, int cityId, int countryId,
            Timestamp resgiterTime, byte[] image, String email) {
        super(id, username);
        setUserProperties(firstName,lastName,pass,gender,birthday,cityId,countryId,image, email);
        this.registerTime = resgiterTime;
    }
    
    private void setUserProperties(String firstName, String lastName, String pass, GenderEnum gender, Calendar birthday, 
            int cityId, int countryId, byte[] image, String email){
        this.firstName = firstName;
        this.lastName = lastName;
        this.password = pass;
        this.gender = gender;
        this.birthday = birthday;
        this.cityID = cityId;
        this.countryID = countryId;
        this.image = image;
        this.email = email;
    }

    public Timestamp getRegisterTime() {
        return registerTime;
    }

    public void setRegisterTime(Timestamp registerTime) {
        this.registerTime = registerTime;
    }

    public byte[] getImage() {
        return image;
    }

    public void setImage(byte[] image) {
        this.image = image;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getPassword() {
        return password;
    }

    public GenderEnum getGender() {
        return gender;
    }

    public Calendar getBirthday() {
        return birthday;
    }

    public int getCityID() {
        return cityID;
    }

    public void addBook(Book book) {

        //initialize array list when first needed
        //(no reason to take up so much space right off the bat):
        if (books == null) {
            books = new ArrayList<Book>();
        }

        if (!books.contains(book)) {
            books.add(book);
        }
    }

    public ArrayList<Book> getBooks() {
        return books;
    }

    public int getCountryID() {
        return countryID;
    }

    public boolean isAdmin() {
        return isAdmin;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setGender(GenderEnum gender) {
        this.gender = gender;
    }

    public void setBirthday(Calendar birthday) {
        this.birthday = birthday;
    }

    public void setCityID(int cityID) {
        this.cityID = cityID;
    }

    public void setCountryID(int countryID) {
        this.countryID = countryID;
    }

    public void setAdmin(boolean isAdmin) {
        this.isAdmin = isAdmin;
    }

    @Override
    public Object[] toObjectArray() {
        return new Object[]{getName(), getFirstName(), getLastName(),
            getPassword(), getGender(), getBirthday(), getCityID(),
            getCountryID(), isAdmin(), getRegisterTime(), getImage(), getEmail()};
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
